//JavaScript Framework 2.0 Code
try {
	Type.registerNamespace('com.yonyou.ncmm_pac_pda.BorrowInMakeListController');
	com.yonyou.ncmm_pac_pda.BorrowInMakeListController = function() {
		com.yonyou.ncmm_pac_pda.BorrowInMakeListController.initializeBase(this);
		this.initialize();
	}

	function com$yonyou$ncmm_pac_pda$BorrowInMakeListController$initialize() {
		//you can programing by $ctx API
		//get the context data through $ctx.get()
		//set the context data through $ctx.push(json)
		//set the field of the context through $ctx.put(fieldName, fieldValue)
		//get the parameter of the context through $ctx.param(parameterName)
		//Demo Code:
		//    var str = $ctx.getString();      //获取当前Context对应的字符串
		//    alert($ctx.getString())          //alert当前Context对应的字符串
		//    var json = $ctx.getJSONObject(); //获取当前Context，返回值为json
		//    json["x"] = "a";        //为当前json增加字段
		//    json["y"] = [];           //为当前json增加数组
		//    $ctx.push(json);            //设置context，并自动调用数据绑定
		//    
		//    put方法需手动调用databind()
		//    var x = $ctx.get("x");    //获取x字段值
		//    $ctx.put("x", "b");     //设置x字段值
		//    $ctx.put("x", "b");     //设置x字段值
		//    $ctx.databind();            //调用数据绑定才能将修改的字段绑定到控件上
		//    var p1 = $param.getString("p1");   //获取参数p2的值，返回一个字符串
		//    var p2 = $param.getJSONObject("p2");   //获取参数p3的值，返回一个JSON对象
		//    var p3 = $param.getJSONArray("p3");   //获取参数p1的值，返回一个数组

		//your initialize code below...

	}

	function com$yonyou$ncmm_pac_pda$BorrowInMakeListController$evaljs(js) {
		eval(js)
	}

	function com$yonyou$ncmm_pac_pda$BorrowInMakeListController$loadScanList(sender, args) {
		loadScanListData();
	}

	//加载到货单自制扫描记录
	function loadScanListData() {
		NCMMPubUtil.queryVOByConditions(BorrowInMakeVO, null, function(data) {
			var showData = eval(data);
			showData = NCMMPubUtil.htmlspecialjsonarray2UI(showData);
			var jsondata = {
				listdata: eval(showData)
			};
			console.log($jsonToString(jsondata));
			$ctx.push(jsondata);
		});
	}

	function com$yonyou$ncmm_pac_pda$BorrowInMakeListController$close(sender, args) {
		$view.close();
	}

	function com$yonyou$ncmm_pac_pda$BorrowInMakeListController$onresume(sender, args) {
		loadScanListData();
	}
	var popDialogInfo = {
		"viewid": "panel_btn",
		"popgravity": "popbottom"
	}

	function com$yonyou$ncmm_pac_pda$BorrowInMakeListController$showDetail(sender, args) {
		$view.closePop(popDialogInfo);
		var row = $id("listview").get("row");
		$view.open({
			"viewid": "com.yonyou.ncmm_pac_pda.BorrowInMakeDetail",
			"isKeep": "true",
			"row": row
		});
	}

	function com$yonyou$ncmm_pac_pda$BorrowInMakeListController$doScan(sender, args) {
		$view.closePop(popDialogInfo);
		var row = $id("listview").get("row");
		$view.open({
			"viewid": "com.yonyou.ncmm_pac_pda.BorrowInMakeEdit",
			"isKeep": "true",
			"row": row
		});
	}

	function com$yonyou$ncmm_pac_pda$BorrowInMakeListController$itemCancel(sender, args) {
		$view.closePop(popDialogInfo);
	}

	function com$yonyou$ncmm_pac_pda$BorrowInMakeListController$itemDelete(sender, args) {
		$view.closePop(popDialogInfo);
		var params = {
			"title": "提示",
			"message": "确定删除所选记录吗？",
			"okbuttontitle": "确定",
			"cancelbuttontitle": "取消",
			"style": "ok-cancel",
			"okaction": "doDelete()",
			"cancelaction": "doCancel()"
		};
		$view.openDialog(params);
	}

	function doDelete() {
		var row = $id("listview").get("row");
		row = $stringToJSON(row);
		NCMMPubUtil.deleteVO(BorrowInMakeVO, row);
		$toast("删除成功。");
		loadScanListData();
	}

	function doCancel() {
		$toast("删除操作已取消。");
	}

	function com$yonyou$ncmm_pac_pda$BorrowInMakeListController$showItemButton(sender, args) {
		$view.openPop(popDialogInfo);
	}

	function com$yonyou$ncmm_pac_pda$BorrowInMakeListController$showMenu(sender, args) {
		NCMMPubMenu.openDropDownList("btn_menu");
	}

	function com$yonyou$ncmm_pac_pda$BorrowInMakeListController$doAllChecked(sender, args) {
		$ctx.dataCollect();
		var isChecked = $id(sender).get("checked");
		var sourceData = $ctx.getJSONArray("listdata");
		for(var i = 0; i < sourceData.length; i++) {
			sourceData[i]["isChecked"] = isChecked;
		}
		var json = {};
		json["listdata"] = sourceData;
		json["isAllChecked"] = isChecked;
		$ctx.push(json);
	}

	function com$yonyou$ncmm_pac_pda$BorrowInMakeListController$doChecked(sender, args) {
		var isCheckedAll = true;
		$ctx.dataCollect();
		var sourceData = $ctx.getJSONArray("listdata");
		for(var i = 0; i < sourceData.length; i++) {
			if(sourceData[i]["isChecked"] == null || sourceData[i]["isChecked"] == false || sourceData[i]["isChecked"] == "false") {
				isCheckedAll = false;
			}
		}
		$ctx.put("scanlist", sourceData);
		$ctx.put("isAllChecked", isCheckedAll);
		$ctx.dataBind();
	}
	//提交
	function com$yonyou$ncmm_pac_pda$BorrowInMakeListController$doCommit(sender, args) {
	   
		$js.showLoadingBar();
		$ctx.dataCollect();
		var sourceData = $ctx.getJSONArray("listdata");
		//$alert($jsonToString(sourceData));
		console.log($jsonToString(sourceData));
		var billinfos = [];
		var needCommit = false;
		var group=[];
		var map={};
		//记录提交的vo
		var scanpks = [];
		//过滤需要提交的单据，并根据仓库以及组织进行分组
		for(var i = 0; i < sourceData.length; i++) {
			if(sourceData[i]["isChecked"] != null && (sourceData[i]["isChecked"] == true || sourceData[i]["isChecked"] == "true")) {
				needCommit = true;
				scanpks.push(sourceData[i]["pk_scan"]);
				//分单
				var headkey = sourceData[i]["pk_org"] + sourceData[i]["cwarehouseid"] + sourceData[i]["ctrantypeid"];
				if(!map[headkey]) {
					group.push({
						"id": headkey,
						"datas": [sourceData[i]]
					});
					map[headkey] = sourceData[i];
				} else {
					for(var j = 0; j < group.length; j++) {
						if(group[j]["id"] == headkey) {
							group[j]["datas"].push(sourceData[i]);
							break;
						}
					}
				}
			}
		}
		if(needCommit == false) {
			$toast("没有选中任何记录！");
			return;
		}
		//$alert($jsonToString(group));
		console.log($jsonToString(group));
		for(var a = 0; a < group.length; a++) {
			var billinfo = {};
			var datas = group[a]["datas"];
			//处理表头
			var head = {};
			head["pk_org"] = datas[0]["pk_org"];
			head["cwarehouseid"] = datas[0]["cwarehouseid"];
			head["ctrantypeid"] = datas[0]["ctrantypeid"];
			billinfo["head"] = head;
			var items = [];
			var itemKeys = [];
			var itemJson = {};
			for(var b = 0; b < datas.length; b++) {
				//行合并
				var itemKey = datas[b]["cmaterialvid"] + datas[b]["vbatchcode"] + datas[b]["castunitid"] + datas[b]["vchangerate"];
				if(NCMMPubUtil.isEmpty(itemJson) || NCMMPubUtil.isEmpty(itemJson[itemKey])) {
					var item = {};
					item["cmaterialvid"] = datas[b]["cmaterialvid"];
					item["nscanastnum"] = datas[b]["nscanastnum"];
					item["nscannum"] = datas[b]["nscannum"];
					item["castunitid"] = datas[b]["castunitid"];
					item["cunitid"] = datas[b]["cunitid"];
					item["vchangerate"] = datas[b]["vchangerate"];
					item["vbatchcode"] = datas[b]["vbatchcode"];
					if(NCMMPubUtil.isNotEmpty(datas[b]["vserialcode"]) || NCMMPubUtil.isNotEmpty(datas[b]["pk_rack"])) {
						var bbitems = [];
						var bbitem = {};
						bbitem["vserialcode"] = datas[b]["vserialcode"];
						bbitem["clocationid"] = datas[b]["pk_rack"];
						bbitem["nnum"] = (datas[b]["nscannum"] == "" ? "1.00000" : datas[b]["nscannum"]);
						bbitem["nassistnum"] = datas[b]["nscanastnum"];
						bbitems.push({
							"bbitem": bbitem
						});
						item["bbitems"] = bbitems;
					}
					itemJson[itemKey] = item;
					//crowno = crowno + 10;
					itemKeys.push(itemKey);
				} else {
					var newItem = itemJson[itemKey];
					newItem.nscannum = NCMMPubUtil.getAddNum(newItem.nscannum, datas[b]["nscannum"]);
					newItem.nscanastnum = NCMMPubUtil.getAddNum(newItem.nscanastnum, datas[b]["nscanastnum"]);
					if(NCMMPubUtil.isNotEmpty(datas[b]["vserialcode"]) || NCMMPubUtil.isNotEmpty(datas[b]["pk_rack"])) {
						var bbitems = newItem.bbitems;
						var bbitem = {};
						bbitem["vserialcode"] = datas[b]["vserialcode"];
						bbitem["clocationid"] = datas[b]["pk_rack"];
						bbitem["nnum"] = (datas[b]["nscannum"] == "" ? "1.00000" : datas[b]["nscannum"]);
						bbitem["nassistnum"] = datas[b]["nscanastnum"];
						bbitems.push({
							"bbitem": bbitem
						});
					}
				}
			}
			for(var c = 0; c < itemKeys.length; c++) {
				var itemjson = {
					"item": itemJson[itemKeys[c]]
				};
				items.push(itemjson);
			}
			billinfo["items"] = items;
			var billjson = {
				"billinfo": billinfo
			};
			billinfos.push(billjson);
		}
		//$alert($jsonToString(billinfos));
		console.log($jsonToString(billinfos));
		BorrowInMakeUtil.commitData(billinfos, scanpks, function(data) {
			loadScanListData();
		});
	}

	com.yonyou.ncmm_pac_pda.BorrowInMakeListController.prototype = {
		doCommit: com$yonyou$ncmm_pac_pda$BorrowInMakeListController$doCommit,
		doChecked: com$yonyou$ncmm_pac_pda$BorrowInMakeListController$doChecked,
		doAllChecked: com$yonyou$ncmm_pac_pda$BorrowInMakeListController$doAllChecked,
		showMenu: com$yonyou$ncmm_pac_pda$BorrowInMakeListController$showMenu,
		showItemButton: com$yonyou$ncmm_pac_pda$BorrowInMakeListController$showItemButton,
		onresume: com$yonyou$ncmm_pac_pda$BorrowInMakeListController$onresume,
		close: com$yonyou$ncmm_pac_pda$BorrowInMakeListController$close,
		loadScanList: com$yonyou$ncmm_pac_pda$BorrowInMakeListController$loadScanList,
		initialize: com$yonyou$ncmm_pac_pda$BorrowInMakeListController$initialize,
		evaljs: com$yonyou$ncmm_pac_pda$BorrowInMakeListController$evaljs,
		doScan: com$yonyou$ncmm_pac_pda$BorrowInMakeListController$doScan,
		showDetail: com$yonyou$ncmm_pac_pda$BorrowInMakeListController$showDetail,
		itemDelete: com$yonyou$ncmm_pac_pda$BorrowInMakeListController$itemDelete,
		itemCancel: com$yonyou$ncmm_pac_pda$BorrowInMakeListController$itemCancel,
	};
	com.yonyou.ncmm_pac_pda.BorrowInMakeListController.registerClass('com.yonyou.ncmm_pac_pda.BorrowInMakeListController', UMP.UI.Mvc.Controller);
} catch(e) {
	$e(e);
}